package com.explorex.infra.puma.gateway.netty.example;


import com.explorex.infra.puma.gateway.netty.common.security.AESUtil;
import com.explorex.infra.puma.gateway.netty.common.security.EDCHUtil;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author: JacceYang
 * @createTime: 2022/08/31 17:43
 * @description: websocket client 端
 */
public class WebSocketClientExample {

    private static final String BEAT_URL = "wss://longlink.explorx.com:443/longlink";
    private static final String LOCAL = "ws://127.0.0.1:5444/longlink";

    private static final KeyPair keyPair = EDCHUtil.generateKeyPair();

    public static void main(String[] args) throws Exception {

        MyWebSocketClient myClient = new MyWebSocketClient(new URI(LOCAL));
        myClient.connect();
        Thread.sleep(10000);
        System.out.println("open=" + myClient.isOpen());

        String authMsg = "{\"header\":{\"type\":1,\"domain\":\"red\"},\"body\":{\"uid\":\"58a16007805d89300a00b761\",\"sid\":\"AT-2641d09ed70442b88f7d66df45db23a1-0ebaedffe12c41a4af5214503a0cbafc\",\"device\":{\"deviceId\":\"14BEC364-BD1E-4E7E-B32C-2A2DAA0E2C5F\",\"platform\":\"browser\",\"os\":\"web\",\"deviceName\":\"iPad mini 2\",\"appVersion\":\"7.47\",\"osVersion\":\"14.4.2\",\"fingerprint\":\"20220606171814d416961e0a11c470c40a182126f17887010222df3bb4f90011\"},\"authType\":\"porch\",\"extra\":\"{}\"}}";

        byte[] encrypt = AESUtil.encrypt(myClient.key, authMsg.getBytes(StandardCharsets.UTF_8));


        myClient.send(new String(encrypt, StandardCharsets.ISO_8859_1));

        myClient.send("{\"header\":{\"type\":3,\"domain\":\"red\",\"contentType\":\"json\",\"bizId\":4,\"seq\":100},\"body\":{\"command\":3,\"roomId\":\"9876\",\"roomType\":\"GOD\",\"priority\":1,\"customData\":null,\"token\":\"token\",\"ts\":1}}");

//        for (int i=0;i<200;i++){
//            myClient.send("{\"header\":{\"type\":3,\"domain\":\"red\",\"contentType\":\"json\",\"bizId\":4,\"seq\":100},\"body\":{\"command\":1,\"roomId\":\"9876\",\"roomType\":\"GOD\",\"priority\":1,\"customData\":\"aGVsbG8=\",\"token\":\"token\",\"ts\":1,\"senderId\":\"58a16007805d89300a00b761\",\"receiverId\":\"207507691180927622\"}}");
//        }
    }


    static class MyWebSocketClient extends WebSocketClient {
        private static final Map<String, String> header = new HashMap<>();

        static {
            header.put("key", Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded()));
        }

        private final AtomicInteger cnt = new AtomicInteger(0);
        private byte[] key;

        public MyWebSocketClient(URI serverUri) {
            super(serverUri, header);
        }

        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            System.out.println("open called");
            String keyStr = serverHandshake.getFieldValue("key");
            byte[] decode = Base64.getDecoder().decode(keyStr);
//            System.out.printf("client recive public key:%s%n", printHexBinary(decode));
            key = EDCHUtil.generateSharKey(decode, keyPair);
        }

        @Override
        public void onMessage(String s) {

            System.out.println("cnt=" + cnt.getAndAdd(1) + ",message=" + s);
        }

        @Override
        public void onClose(int i, String s, boolean b) {

        }

        @Override
        public void onError(Exception e) {

        }
    }
}
